package com.tchintech.pay.view.certification;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;

import com.google.gson.Gson;
import com.tchintech.pay.R;
import com.tchintech.pay.delegate.certification.ShoperInfoDelegate;
import com.tchintech.pay.frame.presenter.ActivityPresenter;
import com.tchintech.pay.model.entity.AuthenticateResult;
import com.tchintech.pay.model.entity.HttpCode;
import com.tchintech.pay.model.entity.ResultBean;
import com.tchintech.pay.net.HttpRequester;
import com.tchintech.pay.utils.AppConst;
import com.tchintech.pay.utils.RegexUtils;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/7/7 0007.
 */

public class ShoperInfoActivity extends ActivityPresenter<ShoperInfoDelegate> implements View.OnClickListener {
    private final String URL_shopInfo = "Merchant/perfect";
    private Map<String,String> map;
    private Handler handler;
    Gson gson=new Gson();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initView() {
        super.initView();
        viewDelegate.setOnClickListener(this,R.id.shoperInfo_bt_confirm, R.id.shoperInfo_bt_cancel);
        handler();
    }


    private void  handler(){
        handler=new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                String message=msg.obj.toString();
                switch (msg.what){
                    case 1:
                        AuthenticateResult authenticateResult =gson.fromJson(message,AuthenticateResult.class);
                        showMsg("完成信息上传");
                        finish();
                        //TODO
                        break;
                    case 2:
                        viewDelegate.setTexts("认证失败，请重新认证");
                        break;
                }
            }
        };
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.shoperInfo_bt_confirm:

                String str_shoperInfoEdShopName = viewDelegate.getTexts(R.id.shoperInfo_ed_shopName);
                String str_shoperInfoEdShopAddress =  viewDelegate.getTexts(R.id.shoperInfo_ed_shopAddress);
                String str_shoperInfoEdShopType = viewDelegate.getTexts(R.id.shoperInfo_ed_shopType);

                if (RegexUtils.checkName(str_shoperInfoEdShopName) && RegexUtils.checkName(str_shoperInfoEdShopType) && RegexUtils.checkName(str_shoperInfoEdShopAddress)) {

                    map = new HashMap<>();

                    map.put("merchantName", str_shoperInfoEdShopName);
                    map.put("merchantAddress", str_shoperInfoEdShopAddress);
                    map.put("commodityType", str_shoperInfoEdShopType);
                    try {
                        httpPost(URL_shopInfo,map, HttpCode.shoper,null);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                } else {
                    //TODO 信息有误
                    viewDelegate.setTexts("信息有误");
                }

                break;
            case R.id.shoperInfo_bt_cancel:
                   finish();
                break;
        }
    }

    /*//网路请求界面配置
    private void httpPost(String url, Map<String, String> map, int what, File file ) {
        String path = HttpRequester.path(url, map);
        try {
            showProgressDialog("", "加载中，请稍后...");
            HttpRequester.post(this, what, path, map, this, file);
        } catch (Exception e) {
            hideProgressDialog();
            showMsg("网络异常");
        }
    }*/


    @Override
    public void httpCallback(String resultBean, int what) {
        super.httpCallback(resultBean, what);
        Message msg=new Message();

        switch (what){
            case HttpCode.shoper:
              /*  if(resultBean.getResCode().equals("1")){
                    msg.obj=resultBean.getResMessage();
                    msg.what=1;
                }else{
                    msg.obj=resultBean.getResMessage();
                    msg.what=2;
                }*/
                break;
        }
        handler.sendMessage(msg);
    }


    @Override
    protected Class<ShoperInfoDelegate> getDelegateClass() {
        return ShoperInfoDelegate.class;
    }
}
